package com.youhong.teethcare.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;

import com.youhong.teethcare.MainActivity;
import com.youhong.teethcare.R;
import com.youhong.teethcare.services.Common;

/**
 * Created by Administrator on 2018/4/10 0010.
 */

public class DialogUtil {

    public static final int REQUEST_ENABLE_BT = 0xA5;

    public static void enableBluetooth(Fragment fragment) {

        Intent enableBtIntent = new Intent(
                BluetoothAdapter.ACTION_REQUEST_ENABLE);
        fragment.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }

    public static void enableBluetooth(Activity activity) {

        Intent enableBtIntent = new Intent(
                BluetoothAdapter.ACTION_REQUEST_ENABLE);
        activity.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }

    public static boolean getBleStatus(Context context) {

        final BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
        BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();
        return mBluetoothAdapter.isEnabled();
    }

    public static AlertDialog modalDialog1;

    public static void dismissModalDialog1() {

        if (modalDialog1 != null) {
            modalDialog1.dismiss();

        }
    }

    public static void showModalDialog(Context context, int messageRes, int titleRes, int yesRes, int noRes, final Runnable yes_runnable, @Nullable final Runnable no_runnable) {

        modalDialog1 = new AlertDialog.Builder(context)
                .setMessage(
                        context.getResources()
                                .getString(messageRes))
                .setIconAttribute(android.R.attr.alertDialogIcon)
                .setTitle(context.getResources().getString(titleRes))
                .setCancelable(false)
                .setPositiveButton(yesRes,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                if (yes_runnable != null)
                                    yes_runnable.run();

                            }
                        })
                .setNegativeButton(noRes,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                if (no_runnable != null)
                                    no_runnable.run();
                            }
                        }).create();

        modalDialog1.show();
    }

    public static void showModalDialog(Context context, String messageText, int titleRes, int yesRes, int noRes, final Runnable yes_runnable, @Nullable final Runnable no_runnable) {

        modalDialog1 = new AlertDialog.Builder(context)
                .setMessage( messageText                 )
                .setIconAttribute(android.R.attr.alertDialogIcon)
                .setTitle(context.getResources().getString(titleRes))
                .setCancelable(false)
                .setPositiveButton(yesRes,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                if (yes_runnable != null)
                                    yes_runnable.run();

                            }
                        })
                .setNegativeButton(noRes,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                if (no_runnable != null)
                                    no_runnable.run();
                            }
                        }).create();

        modalDialog1.show();
    }


    public static ProgressDialog showDialog(Activity context, int messageRes) {

        ProgressDialog progressDialog = new ProgressDialog(context);

        progressDialog.setMessage(context.getResources()
                .getString(messageRes));
        progressDialog.setCancelable(false);

        return progressDialog;
    }

    public static ProgressDialog showDialog(Fragment context, int messageRes) {

        ProgressDialog progressDialog = new ProgressDialog(context.getActivity());

        progressDialog.setMessage(context.getResources()
                .getString(messageRes));
        progressDialog.setCancelable(false);

        return progressDialog;
    }

    public static void showModalDialog(Context context, int messageRes, int titleRes, final Runnable yes_runnable) {

        new AlertDialog.Builder(context)
                .setMessage(
                        context.getResources()
                                .getString(messageRes))

                .setTitle(context.getResources().getString(titleRes))
                .setIconAttribute(android.R.attr.alertDialogIcon)
                .setCancelable(false)
                .setPositiveButton(android.R.string.ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                if (yes_runnable != null)
                                    yes_runnable.run();

                            }
                        })
                .create().show();
    }
}
